package com.guagua.chat.topic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.guagua.util.HazelcastCollectionUtil;
import com.guagua.vas.constants.CacheConstants;
import com.guagua.vas.redis.RedIsHandlerFactory;
import com.guagua.vas.util.StringUtil;

/**
 * 北京八度互联科技有限公司版权所有
 * Copyright (C) Badu Corporation. All Rights Reserved
 */
/** ******************************************************************
* 类概要:说说管理类
* @author 李一
* @version 2012-7-2 9:00:00
*******************************************************************/
public class TopicManager {
	

	private static final TopicManager _topicManager = new TopicManager();
	private static final String HAZELCASE_MAPNAME ="ROOM_HOMEPAGE_SHUOSHUO";//缓存MAPNAME
	private static final String HAZELCASE_COMMENT_MAPNAME="ROOM_HOMEPAGE_SHUOSHUO_COMMENT";//说说评论缓存MAPNAME
	private static final int _ROWS = 10;//每页显示几行
	private TopicDao topicDao = new TopicDao();
	
	private TopicManager(){}
	
	/**
	 * 单例模式-返回实例
	 * @return TopicManager
	 */
	public static TopicManager getInstance(){
		return _topicManager;
	}
	
	
	/**
	 * 新增说说主题或对主题进行评论
	 * @param topicInfo
	 * @param roomId
	 * @return
	 */
	public boolean createTopicOrComment(TopicInfo topicInfo,String roomId){
		HazelcastCollectionUtil.destory(this.HAZELCASE_MAPNAME+"_"+roomId);
		if(topicInfo.getParent_id()!=0L){
			HazelcastCollectionUtil.destory(this.HAZELCASE_COMMENT_MAPNAME+"_"+roomId+"_"+topicInfo.getParent_id());
		}
		return topicDao.createTopicOrComment(topicInfo, roomId);
	}
	/**
	 * 返回说说分页数据
	 * @param roomId
	 * @param pageNo
	 * @return map list-data:分页数据  count:数量
	 * @throws Exception
	 */
	public Map<String,Object> findTopicAllByRoomId(String roomId,int pageNo) throws Exception{
		Map<String,Object> map = null;
		String key ="SHUOSHUOLIST";
		if(pageNo == 1){//首页从缓存中获取
			Map _temp = HazelcastCollectionUtil.getMap(this.HAZELCASE_MAPNAME+"_"+roomId);
			if(_temp!=null && _temp.get(key)!=null){//取到数据
				map = (Map<String,Object>)_temp.get(key);
			}
		}
		if(map == null){//从数据库中读取
			map = new HashMap();
			List<TopicInfo> list = topicDao.findTopicsByRoomId(roomId, pageNo, _ROWS);//数据
			int count = topicDao.getTopicCountByRoomId(roomId);
			map.put("listdata", list);
			map.put("count", count);
			//放入缓存
			if(pageNo == 1){
				HazelcastCollectionUtil.putMapWithTime(HAZELCASE_MAPNAME+"_"+roomId, key, map, 30*60L, TimeUnit.SECONDS);
			}
			
		}
		
		return map;
	}
	/**
	 * 删除说说主题
	 * @param roomId
	 * @param id
	 * @return
	 */
	public boolean delTopicById(String roomId,long id){
		//删除主题时清除缓存
		HazelcastCollectionUtil.destory(this.HAZELCASE_MAPNAME+"_"+roomId);
		return topicDao.delTopicById(id, roomId);
	}
	/**
	 * 返回评论分页数据
	 * @param roomId
	 * @param topicId
	 * @param pageNo
	 * @return
	 * @throws Exception
	 */
	public Map<String,Object> findTopicCommentsByRoomId(String roomId,long topicId,int pageNo)throws Exception{
		String key ="SHUOSHUOCOMMENTLIST";
		Map<String,Object> map =null;
		if(pageNo == 1){
			Map _temp = HazelcastCollectionUtil.getMap(this.HAZELCASE_COMMENT_MAPNAME+"_"+roomId+"_"+topicId);
			if(_temp!=null && _temp.get(key)!=null){//取到数据
				map = (Map<String,Object>)_temp.get(key);
			}
		}
		if(map == null){//从数据库中读取
			map = new HashMap();
			List<TopicInfo> list = topicDao.findTopicCommentsByRoomId(roomId, topicId, pageNo, _ROWS);//数据
			int count = topicDao.getCommentCountByTopicId(roomId, topicId);
			map.put("listdata", list);
			map.put("count", count);
			//放入缓存
			if(pageNo == 1){
				HazelcastCollectionUtil.putMapWithTime(this.HAZELCASE_COMMENT_MAPNAME+"_"+roomId+"_"+topicId, key, map, 10*60L, TimeUnit.SECONDS);
			}
		}
		
		return map;
	}
	/**
	 * 删除评论
	 * @param id
	 * @param parentId
	 * @param roomId
	 * @return
	 */
	public boolean delCommentById(long id,long parentId,String roomId){
		HazelcastCollectionUtil.destory(this.HAZELCASE_MAPNAME+"_"+roomId);
		HazelcastCollectionUtil.destory(this.HAZELCASE_COMMENT_MAPNAME+"_"+roomId+"_"+parentId);
		return topicDao.delCommentById(id, parentId,roomId);
	}
	/**
	 * 是否为超级管理员
	 * @param userId
	 * @param roomId
	 * @return
	 */
	public int isSuperAdmin(long userId,String roomId){
		Map<String,String> mapRoomMsg = null;
		//从公用redis中查询房间的基本信息，主要是获取房间名称和室主
		mapRoomMsg = RedIsHandlerFactory.getInstance().getGuaguaRoomMsgMap(Long.parseLong(roomId));
		if(mapRoomMsg!=null && mapRoomMsg.size()>0){
			String categoryIds = mapRoomMsg.get("category_id");
			if(categoryIds!=null){
				Map<String,String> superAdminMap = RedIsHandlerFactory.getInstance().getSuperAdmins();
				String[] categoryIdArray=categoryIds.split(",");
				for(String categoryId : categoryIdArray){
					if(superAdminMap.get("guagua_supadmin"+categoryId+userId)!=null){
						return 1;
					}
				}
			}
		}
		return 0;
	}
	
	
	
}
